/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package filer;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import webservice.amazon.ItemSearch;

/**
 *
 * @author maggie
 */
public class Main {

 	private static UrlShortcutFactory usf = new UrlShortcutFactory();

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
		try {
			// TODO code application logic here
			/* File Iterator
	//        String target = "G:\\画像\\001_20110730111425.jpg";
			String target = "G:\\画像\\ひとゆめ";
			File[] f = new File[1];
			f[0] = new File(target);
			listPrint(f);
			*/
			/* Webloc
			String filePath = "F:\\NewNas\\DATA\\MacOS\\Desktop\\URLMemo\\【画像あり】すーぱーそに子のおっぱいが挑発的過ぎる │ キニ速　　気になる速報.webloc";
			Webloc w = new Webloc(filePath);
			System.out.println("Title:[" + w.getTitle() + "]");
			System.out.println("URL:[" + w.getUrl() + "]");
			*/
			/* IniFile(.url,.website)
			String filePath = "C:\\Users\\maggie\\Desktop\\二次元画像詳細検索 似ている画像 2c8f549edfd565053ff46bdf81a86b29.url";
			IniFile i = new IniFile(filePath);
			System.out.println("Title:[" + i.getTitle() + "]");
			System.out.println("URL:[" + i.getUrl() + "]");
			*/
			/* Factory
			UrlShortcutFactory f = new UrlShortcutFactory();
			UrlShortcut s1 = f.getInstance("F:\\NewNas\\DATA\\MacOS\\Desktop\\URLMemo\\【画像あり】すーぱーそに子のおっぱいが挑発的過ぎる │ キニ速　　気になる速報.webloc");
			System.out.println("Title:[" + s1.getTitle() + "]");
			System.out.println("URL:[" + s1.getUrl() + "]");
			UrlShortcut s2 = f.getInstance("C:\\Users\\maggie\\Desktop\\二次元画像詳細検索 似ている画像 2c8f549edfd565053ff46bdf81a86b29.url");
			System.out.println("Title:[" + s2.getTitle() + "]");
			System.out.println("URL:[" + s2.getUrl() + "]");
			UrlShortcut s3 = f.getInstance("C:\\Users\\maggie\\Desktop\\HP  MSN.website");
			System.out.println("Title:[" + s3.getTitle() + "]");
			System.out.println("URL:[" + s3.getUrl() + "]");
			*/
			// One Directory
					/*
					String target = "F:\\NewNas\\DATA\\MacOS\\Desktop";
					File f = new File(target);
					urlList(f);
					*/
					// Amazon
					/*
					String id = "4562160491457";
					ItemSearch is = new ItemSearch();
					Map<String, String> res = is.getResponse(id);
					System.out.println("Title;[" + res.get("Title") + ']');
					System.out.println("DetailPageURL;[" + res.get("DetailPageURL") + ']');
					System.out.println("ASIN;[" + res.get("ASIN") + ']');
					System.out.println("LargeImage;[" + res.get("LargeImage") + ']');
					System.out.println("Binding;[" + res.get("Binding") + ']');
					System.out.println("Brand;[" + res.get("Brand") + ']');
					System.out.println("IsAdultProduct;[" + res.get("IsAdultProduct") + ']');
					System.out.println("PriceAmount;[" + res.get("PriceAmount") + ']');
					System.out.println("PriceCurrency;[" + res.get("PriceCurrency") + ']');
					System.out.println("Platform;[" + res.get("Platform") + ']');
					System.out.println("ReleaseDate;[" + res.get("ReleaseDate") + ']');
					*/
					// CSV
					CsvFile csv = new CsvFile("C:\\Users\\maggie\\Desktop\\20120831.csv");
					Map<String, String> line;
					String keyword;
					int index = 0;
					while(csv.hasNext()) {
						line = (Map<String, String>)csv.next();
//						System.out.println("Keyword[" + line.get("ISBN") + ']');
						keyword = line.get("ISBN");
						index++;
						System.out.println("Now->" + index + ":[" + keyword + ']');
						ItemSearch is = new ItemSearch();
						Map<String, String> res = is.getResponse(keyword);
						line.putAll(res);
						csv.update(line);
					}
					csv.flash("C:\\Users\\maggie\\Desktop\\test002.csv");
		} catch (IOException ex) {
			Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
		}
				

    }

    public static void listPrint(File fs[]) {
        for(File f : fs) {
            System.out.println(f.getName());
            if(f.isDirectory()) {
                listPrint(f.listFiles());
            }
        }
    }

	public static void urlList(File fs) {
		UrlShortcut sc;

		for (File f:fs.listFiles()) {
			if (f.isDirectory()) {
				urlList(f);
			} else {
				sc = Main.usf.getInstance(f.getAbsolutePath());
				if (null != sc) {
					filePrint(sc);
				}
			}
		}
	}

	public static void filePrint(UrlShortcut sc) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		String dateString;
		System.out.println("************************");
		System.out.println("Title:[" + sc.getTitle() + "]");
		System.out.println("URL:[" + sc.getUrl() + "]");
		dateString = df.format(new Date(sc.lastModified));
		System.out.println("LastModified:[" + dateString + "]");
		System.out.println("************************");

	}
}
